﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 文件对话框
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 将指定目录下的某一txt文档内容读到textbox中显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 导入ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Multiselect = false;
            ofd.Title = "请选择你要导入的txt文件";
            ofd.InitialDirectory = ".";
            ofd.Filter = "文本文件|*.txt";
            ofd.ShowDialog();
            string path = ofd.FileName;
            if (path == "")
            {
                return;
            }
            File.Copy(path,"test.txt",true);
            path = "test.txt";
            using (FileStream fr = new FileStream(path, FileMode.Open, FileAccess.ReadWrite))
            {
                FileInfo fi = new FileInfo(path);
                int filesize = (int)fi.Length;
                byte [] buff = new byte[filesize];
                fr.Read(buff, 0, filesize);
                textBox1.WordWrap = true;
                textBox1.Text = Encoding.Default.GetString(buff);
            }
        }

        /// <summary>
        /// 将textbox里的内容保存到应用程序目录下的test.txt文件中
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (FileStream fw = new FileStream("test.txt", FileMode.OpenOrCreate, FileAccess.Write))
            {
                byte[] buff = Encoding.Default.GetBytes(textBox1.Text);
                fw.Write(buff, 0, buff.Length);
            }
        }

        /// <summary>
        /// 将listbox里的内容另存到新的txt文件中
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Title = "请选择你要另存为的位置";
            sfd.InitialDirectory = Application.StartupPath;
            sfd.OverwritePrompt = true;
            sfd.AddExtension = true;
            sfd.DefaultExt = ".txt";
            sfd.Filter = "文本文件|*.txt";
            sfd.ShowDialog();

            string path = sfd.FileName;
            if (path == "")
            {
                return;
            }
            else
            {
                using(FileStream fw = new FileStream(path,FileMode.OpenOrCreate,FileAccess.Write))
                {
                    byte[] buff = Encoding.Default.GetBytes(textBox1.Text);
                    fw.Write(buff,0,buff.Length);
                }
            }

        }

        private void 自动换行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (自动换行ToolStripMenuItem.Text == "☆自动换行")
            {
                textBox1.WordWrap = true;
                自动换行ToolStripMenuItem.Text = "★取消自动换行";
            }
            else if (自动换行ToolStripMenuItem.Text == "★取消自动换行")
            {
                textBox1.WordWrap = false;
                自动换行ToolStripMenuItem.Text = "☆自动换行";
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.WordWrap = false;
        }
    }
}
